package de.baumann.browser.fragments;

import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.EditText;

import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

import de.baumann.browser.R;
import de.baumann.browser.adapter.Adapter_Record;
import de.baumann.browser.base.BasePresenter;
import de.baumann.browser.db.Record;
import de.baumann.browser.db.RecordAction;
import de.baumann.browser.iview.IBaseView;
import de.baumann.browser.units.BrowserUnit;
import de.baumann.browser.views.widget.OdinToast;


/**
 * @author：
 * @date：2018/9/26
 * @discription: 书签列表
 **/
public class BookmarkFragment extends BaseBHFragment {
    @Override
    public void initView(View view) {
        super.initView(view);

        mListView.setOnItemLongClickListener((parent, view1, position, id) -> {
            showListMenu((Adapter_Record) parent.getAdapter(), list, position, BrowserUnit.FLAG_BOOKMARKS);
            return true;
        });
    }

    @Override
    protected void loadData() {
        list = action.listBookmarks();
        Collections.sort(list, (first, second) -> first.getTitle().compareToIgnoreCase(second.getTitle()));
    }


    @Override
    protected void showEditDialog(final Adapter_Record adapterRecord, List<Record> recordList, int location) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View dialogView = View.inflate(getActivity(), R.layout.dialog_edit, null);

        final Record record = recordList.get(location);
        final EditText editText = dialogView.findViewById(R.id.dialog_edit);

        editText.setHint(R.string.dialog_title_hint);
        editText.setText(record.getTitle());

        builder.setView(dialogView);
        builder.setTitle(R.string.menu_edit);
        builder.setPositiveButton(R.string.app_ok, (dialog, whichButton) -> {

            String text = editText.getText().toString().trim();
            if (text.isEmpty()) {
                OdinToast.show(getActivity(), getString(R.string.toast_input_empty));
            }

            RecordAction action = new RecordAction(getActivity());
            action.open(true);
            record.setTitle(text);
            action.updateBookmark(record);
            action.close();

            adapterRecord.notifyDataSetChanged();
//                hideSoftInput(editText);
        });
        builder.setNegativeButton(R.string.app_cancel, (dialog, whichButton) -> {
            dialog.cancel();
//                hideSoftInput(editText);
        });

        AlertDialog dialog = builder.create();
        dialog.show();
//        showSoftInput(editText);
    }

    @Override
    public void initData() {

    }

    @NotNull
    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @NotNull
    @Override
    public IBaseView createView() {
        return null;
    }
}
